<?php
// used for upload and assign photo to item
// item is xoopsobject class with id field

function neosystem_admin_imagephotos_upload(&$item,$handler='',$module=''){
	global $xoopsUser;
	  include_once XOOPS_ROOT_PATH.'/class/uploader.php';
			$image_handler =& xoops_gethandler('image');

$xoopsimagecategoryH = & xoops_gethandler("imagecategory");
  $imagecatH = & xoops_getmodulehandler("imagecat",'neosystem');
  $imagecats = $imagecatH->getObjects(null,true);

$uploader = new XoopsMediaUploader(XOOPS_UPLOAD_PATH, array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png', 'image/png'),200000);
	$uploader->setPrefix("tmp");
	for ($i=0 ;$i<count($_FILES['item_photo']['name']);$i++) 
    if ($uploader->fetchMedia('item_photo',$i)) {
        if (!$uploader->upload()) {
            $err = $uploader->getErrors();
        } else {

        		if (preg_match("/\.([a-zA-Z0-9]+)$/", $uploader->getSavedFileName(), $matched))  {$ext = $matched[1];}

			$filename_base = $module.'_'.$handler.$item->getVar("id")."_".uniqid("").'.'.$ext;
         foreach ($imagecats as $imagecatO){

			
		 	$prefix = $imagecatO->getVar("prefix");
#			echo $prefix;
			
			$filename = $prefix.$filename_base;
#			copy(XOOPS_UPLOAD_PATH."/".$uploader->getSavedFileName(),XOOPS_UPLOAD_PATH."/".$filename);
#			copy()
			$command = XOOPS_ROOT_PATH."/modules/neosystem/bin/create_thumb.pl  -filename ".XOOPS_UPLOAD_PATH.'/'.$uploader->getSavedFileName()." -outfile ".XOOPS_UPLOAD_PATH."/".$filename." -size ".$imagecatO->getVar("width");
		echo $command;			
			system("$command 2>&1");
			
#		    $uploader->setPrefix($prefix);
		     #$uploader->targetFileName = uniqid($prefix);
		    $xoopsimgcat = $xoopsimagecategoryH->get($imagecatO->getVar("xoopsimagecat"));
            $image =& $image_handler->create();
            $image->setVar('image_name', $filename);
            $image->setVar('image_nicename', $prefix.$uploader->mediaName);
            $image->setVar('image_mimetype', $uploader->getMediaType());
            $image->setVar('image_created', time());
            $image->setVar('image_display', 1);
            $image->setVar('image_weight', 0);
            $image->setVar('imgcat_id', $xoopsimgcat->getVar("imgcat_id"));
            if ($xoopsimgcat->getVar('imgcat_storetype') == 'db') {
                $fp = @fopen($uploader->getSavedDestination(), 'rb');
                $fbinary = @fread($fp, filesize($uploader->getSavedDestination()));
                @fclose($fp);
                $image->setVar('image_body', addslashes($fbinary));
                @unlink($uploader->getSavedDestination());
            }
            if (!$image_handler->insert($image)) {
                $err = sprintf(_FAILSAVEIMG, $image->getVar('image_nicename'));
            }
			if (!($imagecatO->getVar("prefix"))) {
				#print_r($item);
#				echo "create xoopsimage2item";
			$xoopsimage2itemH = & xoops_getmodulehandler('xoopsimage2item','neosystem');
			$xoopsimage2itemO = $xoopsimage2itemH->create();
			#print_r($xoopsimage2itemO);
#				echo "item_id-".$item->getVar("id");
			$xoopsimage2itemO->setVar("item_id",$item->getVar("id"));
			$xoopsimage2itemO->setVar("xoopsimage_id",$image->getVar("image_id"));			
			$xoopsimage2itemO->setVar("handler",$handler);
			$xoopsimage2itemO->setVar("module",$module);			
			if (is_object($xoopsUser)) {
			$xoopsimage2itemO->setVar("uid",$xoopsUser->getVar("uid"));						
			}
			$xoopsimage2itemH->insert($xoopsimage2itemO);
			}
		 } //foreach	
 			unlink(XOOPS_UPLOAD_PATH."/".$uploader->getSavedFileName());
		}
    } else {
        $err = sprintf(_FAILFETCHIMG, 0);
        $err .= '<br />'.implode('<br />', $uploader->getErrors(false));
    }
#} // for
		echo $err;
}

?>